// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { Download } from 'playwright';
import {
    detailsViewSelectors,
    fastPassReportSelectors,
} from 'tests/end-to-end/common/element-identifiers/details-view-selectors';
import { HtmlReportPage } from 'tests/end-to-end/common/page-controllers/html-report-page';
import { Browser } from '../../common/browser';
import { launchBrowser } from '../../common/browser-factory';
import { DetailsViewPage } from '../../common/page-controllers/details-view-page';
import { TargetPage } from '../../common/page-controllers/target-page';
import { scanForAccessibilityIssues } from '../../common/scan-for-accessibility-issues';

describe('Details View -> FastPass -> Report', () => {
    let browser: Browser;
    let targetPage: TargetPage;
    let detailsViewPage: DetailsViewPage;
    let reportPage: HtmlReportPage;
    const reportSaveAsFilePath = 'test-results/e2e/downloads/fast-pass-report.html';
    let reportDownload: Download;

    beforeAll(async () => {
        browser = await launchBrowser({
            suppressFirstTimeDialog: true,
            addExtraPermissionsToManifest: 'all-origins',
        });
        targetPage = await browser.newTargetPage();
        await browser.newPopupPage(targetPage); // Required for the details view to register as having permissions/being open
        detailsViewPage = await browser.newDetailsViewPage(targetPage);
        await detailsViewPage.waitForSelector(detailsViewSelectors.automatedChecksResultSection, {
            timeout: 10000,
        });
        await detailsViewPage.openTabStopsPage(detailsViewPage);
        await detailsViewPage.addFailedTabStopsInstance(
            detailsViewPage,
            'this is a test failure instance',
        );
        await detailsViewPage.clickSelector(detailsViewSelectors.exportReportButton);
        await detailsViewPage.waitForSelector(detailsViewSelectors.singleExportToHtmlButton);
        reportDownload = await detailsViewPage.downloadExportReport(
            detailsViewSelectors.singleExportToHtmlButton,
            reportSaveAsFilePath,
        );
        reportPage = await browser.newHtmlReportPage(
            `file:${path.join(__dirname, '../../../../../', reportSaveAsFilePath)}`,
        );
    });

    afterAll(async () => {
        try {
            // delete GUID file auto-generated by the download
            await detailsViewPage.deleteDownloadedFile(reportDownload);
            // delete renamed HTML used to open report for testing
            await detailsViewPage.deleteFile(reportSaveAsFilePath);
        } catch (error) {
            console.error(error);
        } finally {
            await browser?.close();
        }
    });

    it('should pass accessibility validation', async () => {
        const results = await scanForAccessibilityIssues(reportPage, '*');
        expect(results).toHaveLength(0);
    });

    it('renders', async () => {
        await reportPage.waitForSelector(fastPassReportSelectors.reportHeaderSection);
    });
});
